﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 12/15/2009
 * Time: 3:35 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.general.views
{
	/// <summary>
	/// Description of frmagenda.
	/// </summary>
	public partial class frmagenda : KryptonForm
	{
		private Contacts listContacts = new Contacts();
		private Contacts listBirthday = new Contacts();
		private Commitments listCommitments = new Commitments();
		private Commitment commitmet = new Commitment();
		private bool updateCommitment = false;
		
		public frmagenda()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			//load();
		}
		
		void BPesquisarClick(object sender, EventArgs e)
		{
			if(rbNome.Checked)
			{
				if(listContacts.searchByName(txtName.Text))
				{
					dgvContacts.Rows[listContacts.getIndex()].Selected = true;
					dgvContacts.FirstDisplayedScrollingRowIndex = listContacts.getIndex();
					dgvContacts.CurrentCell = dgvContacts.Rows[listContacts.getIndex()].Cells[1];
				}
			}
			else
			{
				if(listContacts.searchByPhone(txtName.Text))
				{
					dgvContacts.Rows[listContacts.getIndex()].Selected = true;
					dgvContacts.FirstDisplayedScrollingRowIndex = listContacts.getIndex();
					dgvContacts.CurrentCell = dgvContacts.Rows[listContacts.getIndex()].Cells[1];
				}	
			}
			
			
		}
		
		void FrmagendaLoad(object sender, EventArgs e)
		{
			timer1.Enabled = true;
			load();
		}
		
		void Timer1Tick(object sender, EventArgs e)
		{
			String sDate = DateTime.Now.Date.ToString("d");
			String sTime = DateTime.Now.ToString("T");
			
			tsslDate.Text = sDate;
			tsslTime.Text = sTime;
		}
		
		void RbTelefoneClick(object sender, EventArgs e)
		{
			lblNome.Text = "Telefone";
		}
		
		void RbNomeClick(object sender, EventArgs e)
		{
			lblNome.Text = "Nome";
		}
		
		private void fillDataGridContacts()
		{
			int tam;
			
			dgvContacts.Rows.Clear();
			tam = listContacts.count();
			
			for(int i = 0; i < tam; i++)
			{
				dgvContacts.Rows.Add(new String[] {listContacts.get(i).id, 
				                     	           listContacts.get(i).name,
				                     	           listContacts.get(i).phonefirst,
				                     	           listContacts.get(i).phonesecond,
				                     	           listContacts.get(i).phonethird,
				                     	           listContacts.get(i).email});
			}
			
		}
		
		private void fillDataGridCommitments()
		{
			int tam;
			
			dgvcommitments.Rows.Clear();
			tam = listCommitments.count();
			
			for(int i = 0; i < tam; i++)
			{
				dgvcommitments.Rows.Add(new String[]{listCommitments.get(i).id,
				                        	         listCommitments.get(i).date,
				                        	         listCommitments.get(i).time,
				                        	         listCommitments.get(i).subject});
				if(Convert.ToBoolean(listCommitments.get(i).Resolved))
				{
					dgvcommitments.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
				}
			}
		}
		
		private void fillDataGridBirthday()
		{
			int tam;
			
			dgvbirthday.Rows.Clear();
			tam = listBirthday.count();
			
			for(int i = 0; i < tam; i++)
			{
				dgvbirthday.Rows.Add(new String[]{ listBirthday.get(i).id,
				                     	           listBirthday.get(i).name,
				                     	           Convert.ToDateTime(listBirthday.get(i).born_date).ToString("dd-MM"),
				                     	           listBirthday.get(i).phonefirst,
				                     	           listBirthday.get(i).phonesecond,
				                     	           listBirthday.get(i).email});
			}
		}
		
		//Carrega todos os formularios Contacts, Birthaday, Commitments
		private void load()
		{
			String sMes = DateTime.Now.ToString("MM");
			
			listContacts.fillList();
			fillDataGridContacts();
			listCommitments.fillList();
			fillDataGridCommitments();
			listBirthday.fillistByBirthdayMonth(sMes);
			fillDataGridBirthday();
		}
		
		void BtnNovoClick(object sender, EventArgs e)
		{
			commitmet.time     = tbHora.Text;
			commitmet.date     = dtpData.Text;
			commitmet.subject  = tbAssunto.Text;
			commitmet.Resolved = cbResolved.Checked.ToString();
			if(!updateCommitment )
			{
				if(commitmet.save())
				{
					KryptonMessageBox.Show("Registro salvo com sucesso");
					load();
				}
				else
				{
					KryptonMessageBox.Show("Erro ao tentar salvar o registro:\n"+commitmet.errorMessage);
				}
			}
			else
			{
				if(commitmet.update())
				{
					KryptonMessageBox.Show("Registro salvo com sucesso");
					load();
				}
				else
				{
					KryptonMessageBox.Show("Erro ao tentar salvar o registro:\n"+commitmet.errorMessage);
				}
			}
		}
		
		void DgvcommitmentsDoubleClick(object sender, EventArgs e)
		{
			updateCommitment = true;
			if(listCommitments.search(dgvcommitments.CurrentRow.Cells["id_commitments"].Value.ToString()))
			{
				commitmet.id     = dgvcommitments.CurrentRow.Cells["id_commitments"].Value.ToString();
				tbHora.Text      = dgvcommitments.CurrentRow.Cells["time"].Value.ToString();
				dtpData.Text     = dgvcommitments.CurrentRow.Cells["date"].Value.ToString();
				tbAssunto.Text   = dgvcommitments.CurrentRow.Cells["subject"].Value.ToString();
				cbResolved.Checked = Convert.ToBoolean(listCommitments.get().Resolved);
			}
		}
		
		void BtnNewClick(object sender, EventArgs e)
		{
			commitmet.clear();
			updateCommitment = false;
			tbHora.Text    = string.Empty;
			dtpData.Text   = string.Empty;
			tbAssunto.Text = string.Empty;
			cbResolved.Checked = false;
		}
		
		void PesquisarClick(object sender, EventArgs e)
		{
			DateTime date = mcCalendar.SelectionStart;
			if(rbDiario.Checked)
			{
				if(cbShowResolved.Checked)
				{
					listCommitments.fillListByDay(date.ToString("yyyy/MM/dd"), true);
				}
				else
				{
					listCommitments.fillListByDay(date.ToString("yyyy/MM/dd"), false);
				}
			}
			else
			{
				if(cbShowResolved.Checked)
				{
					listCommitments.fillListByMonth(date.Month.ToString(),date.Year.ToString(), true);
				}
				else
				{
					listCommitments.fillListByMonth(date.Month.ToString(),date.Year.ToString(), false);
				}
			}
			fillDataGridCommitments();
		}
		
		void BtnListarClick(object sender, EventArgs e)
		{
			DateTime date = mcCalendar.SelectionStart;
			
			if(rbDia.Checked)
			{
				listBirthday.fillListByBirthdayDay(date.Day.ToString(), date.Month.ToString());
			}
			else if(rbMes.Checked)
			{
				listBirthday.fillistByBirthdayMonth(date.Month.ToString());
			}
			else
			{
				listBirthday.fillistByBirthdayAll();
			}
			fillDataGridBirthday();
		}
		
		void BtnSearchBirthdayClick(object sender, EventArgs e)
		{
			common.Util.searchInGrid(dgvbirthday, "nome_aniversariante", txtNameBirthday.Text);
		}
	}
}
